﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;

namespace ObjRec
{
    public static class SaveLoad
    {
        public static void Save(string path, object o)
        {
            try
            {
                if (!File.Exists(path))
                {
                    FileStream fs = File.Create(path);
                    fs.Flush();
                    fs.Close();
                }
                string _text = JsonConvert.SerializeObject(o);
                File.WriteAllText(path, _text, Encoding.UTF8);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        public static Dictionary<int, Person> Load(string path)
        {
            Dictionary<int, Person> h = new Dictionary<int,Person>();
            try
            {
                string _text = File.ReadAllText(path, Encoding.UTF8);
                h = JsonConvert.DeserializeObject<Dictionary<int, Person>>(_text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return h;
        }
    }
}
